<?php

/**
 * Autoload
 *
 * @package Books
 * @author  sanli <1326803576@qq.com>
 * @time    2016/04/11 22:27
 * @since   5.3
 */

spl_autoload_register(function ($className) {
    $className = ltrim($className, '\\');
    $fileName = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }

    $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php';
    if (file_exists($fileName)) {
        include_once $fileName;
        return true;
    }

    return false;
});
